/*
	DSA Project on use of arrays and linked lists to obtain the same functionality as the functions of list data structure in Python
*/


#include <stdio.h>
#include<malloc.h>
#include<math.h>

/*
	<list>.append(x)
	Add an item to the end of the list; equivalent to a[len(a):] = [x].
*/

int append(int *list,int var,int buff)
{ 
	int i;
	list=(int*)realloc(list,(buff+1)*sizeof(int));//The realloc() function changes the size of a block of memory that we earlier allocated with 		malloc()
	list[buff]=var;//Consider size of the list(array is what we are talking about)
	for (i=0;i < buff+1;i++)
	{
		printf("%d ", list[i]);
	}
}


/*
	<list>.sort()
    	Sort the items of the list, in place.
*/
int sortinc(int *list1, int buff)
{
	/*
		Here we are taking an element and then comparing it with all the other elements to check if it is bigger or smaller than them and that 			is how we do sorting. We then decide if we want to sort the array in an increasing or decreasing order based on user choice. This 			algorithm is doing the sorting in place.
	*/ 	
	
	int i,j;
	int sort=0;
	int option;
	
	for(i=0;i<buff;i++)
	{
		for(j=i+1;j<buff;j++)
		{
			if(list1[j]<list1[i])//in case that is the case then we swap the values
			{
				sort = list1[i];
				list1[i] = list1[j];
			 	list1[j] = sort;
			}
		}	
	}
	for (i=0;i<buff;i++)
	{
		printf("%d  ", list1[i]);
		printf("%d  ", list1[i]);
		printf("%d  ", list1[i]);
	}
	
	
}

int sortdec(int *list2,int buff)
{
	/*
		Here we are sorting the elements in a decreasing order
	*/ 	
	
	int i,j;
	int sort=0;
	int option;
	
	for(i=0;i<buff;i++)
	{
		for(j=i+1;j<buff;j++)
		{
			if(list2[j]>list2[i])//in case that is the case then we swap the values
			{
				sort = list2[i];
				list2[i] = list2[j];
			 	list2[j] = sort;
			}
		}	
	}
	for (i=0;i<buff;i++)
	{
		printf("%d  ", list2[i]);
	}
}

/*
	<list>.reverse()
	Reverses the list
*/

void reverse(int *list4,int buff)
{	int i;
	int j=buff;
	int swap=0;
	for (i=0;i<--j;i++)//increment i and decrement j until they meet each other
		{
			swap=list4[i];
			list4[i]=list4[j];
		 	list4[j]=swap;
		}
	for (i=0; i<buff;i++)
		{
			printf("%d  ", list4[i]);
		}
}

/*
	<list>.remove(x)
    	Remove the first item from the list whose value is x. It is an error if there is no such item.
*/

void removeelement(int *list5,int number,int buff)
{	int i,j;
	int count=0;
	for (i=0;i<buff;i++)
	{
		if (list5[i]==number)//number is what is we want to be removed from the array 
		{
			count=1; 
			list5=(int*)realloc(list5,(buff-1)*(sizeof(int)));
			break;
		}
	}
 	for(j=i+1;j<buff;j++)
        {
		list5[j-1]=list5[j];
	}
	for (i=0; i<buff-1;i++)
	{
		printf("%d  ",list5[i]);
	}	
}

/*
	<list>.count(x)
	Returns the number of occurences of x in list
*/

void counting(int *array,int number,int buff)
{ 	int i;
	int count=0;
	for (i=0;i<buff;i++)
	{ 
		if (array[i]==number)
		{
			count=count+1; 
		}
	}
	if(count==0)
	{
		printf("\nNumber you asked for is not in the list");
	}
	else 
	{
		printf("\nOccurence of the number is %d",count);
	} 
}

/*
	<list>.pop(x)
	Deletes the ith element of the list and returns its value
*/

int pop(int *list7,int number,int buff)
{ 	
	int i;
	int value;
	list7=(int*)realloc(list7,(buff-1)*(sizeof(int)));//using realloc to ensure that size= size -1 now after the popping of element
	value = list7[number];
	for (i=number+1;i<buff;i++)
	{
		list7[i-1]=list7[i];
	}
	for(i=0;i<buff-1;i++)
	{
		printf("%d ",list7[i]);
	}
	return value;
} 

/*
	<list>.insert(i,x)
	Insert x into list at index i
*/

void insertelement(int *list8,int number,int buff,int index)
{	
	int i;
	list8=(int*)realloc(list8,(buff+1)*sizeof(int));
	
	for(i=buff;i>index-1;i--)
	{
		list8[i+1]=list8[i];
	}
	list8[index]=number;	
	for(i=0;i<buff+1;i++)
	{
		printf("%d  ",list8[i]);
	}
}

/*
	<list>.index(x)
	Returns index of first occurence of x
*/

void indexofelement(int *list9,int buff,int number)
{	
	int i;
	int position=0;
	for(i=0;i<buff;i++)
	{ 
		if (list9[i]==number)
		{
			printf("The index of the number is %d",i);
			position=1; 
			break;	
		}
	}

	if(position!=1)
	{
		printf("Number is not in the given list");
	}	
}

int main ()
{
	
	int *list,buff,var,choice;
	int number;
	int i,e;
	int index;
	int j=0;//this declaration is done only so that it can be used by us in ensuring that the user gets options to do what he wants any number of 		times
	printf("enter the size of the array");
	scanf("%d",&buff);
	list=(int*)(malloc(buff*sizeof(int)));
	for (i=0; i<buff;i++)
	{printf("enter the %d element",i+1);
	scanf("%d", &list[i]); }
	while(j==0)
	{
	printf("\nEnter your option:\n0.exit\n1.append\n2.sort in increasing order\n3.sort in decreasing order\n4.reversing the array\n5.Removing 		element from array\n6.counting\n7.Popping the element\n8.Insert an element\n9.Index of the element");
	scanf("%d",&choice);
	switch(choice)
	{ 
		case 0: exit(0);
			break;
		case 1: printf("Enter the no you want to append at the end of the list:");
			scanf("%d",&var);
		   	append(list,var,buff);
		   	buff =buff+1;
		   	break;
		case 2: sortinc(list,buff);
			break;
		case 3: sortdec(list,buff);
			break;
		case 4: reverse(list,buff);
			break;
		case 5: printf("Enter the number to be removed from the list:");
		   	scanf("%d",&number);
		   	removeelement(list,number,buff);
		   	buff=buff-1;
		   	break;
		case 6: printf("Enter number whose count is required:");
		   	scanf("%d",&number);
		   	counting(list,number,buff);
		   	break;
		case 7: printf("Enter the index from which element is to be popped:");
		   	scanf("%d",&number);
		   	e = pop(list,number,buff);
			printf("The value popped is %d", e);
		   	buff=buff-1;
		   	break;
		case 8: printf("Enter the number to be inserted");
		   	scanf("%d",&number);
		   	printf("Enter the index at which you want to insert");
		   	scanf("%d",&index);
		   	insertelement(list,number,buff,index-1);
		   	buff=buff+1;
		   	break;
		case 9: printf("Enter the number whose index is wanted:");
		   	scanf("%d",&number);
		   	indexofelement(list,buff,number);
		   	break;
	
	}
	}
}


//References: http://www.java-samples.com/showtutorial.php?tutorialid=589 	
